package com.jadefashion.controller;

import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.*;
import com.jadefashion.pojo.CheckGroup;
import com.jadefashion.pojo.MulitObjectBean;
import com.jadefashion.pojo.OrderSetting;
import com.jadefashion.pojo.Setmeal;
import com.jadefashion.service.CheckGroupService;
import com.jadefashion.service.OrderSettingService;
import com.jadefashion.service.SetmealService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;


    @RequestMapping("findDate.do")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Object findDate(@RequestParam("year") Integer year,@RequestParam("month") Integer month,@RequestParam("day") Integer day) {
        Result result = null;
        try {
            List<Map<String,Object>> map=orderSettingService.findDate(year,month,day);
            result = new Result(true, MessageConstant.QUERY_ORDERSETTING_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, MessageConstant.QUERY_ORDERSETTING_FAIL);
        }
        return result;
    }

    @RequestMapping("updateNumber.do")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Object updateNumber(@RequestParam("number") Integer number,@RequestParam("date") String date){
        try {
            orderSettingService.update(number,date);
            return new Result(true,MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(true,MessageConstant.EDIT_ORDERSETTING_FAIL);
        }
    }


    @RequestMapping("importOrderSettings.do")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Object importOrderSettings(@RequestParam("excelFile") MultipartFile file) {
        Result result=null;
        List<OrderSetting> orderSettingListList=new ArrayList<>();
        try {
            List<String[]> list = POIUtils.readExcel(file);
            for (String[] strings : list) {
                OrderSetting orderSetting = new OrderSetting();
                Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");
                orderSetting.setOrderDate(date);
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSettingListList.add(orderSetting);
            }
            orderSettingService.add(orderSettingListList);
            result=new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            result=new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
            e.printStackTrace();
        }
        return result;
    }

}
